package com.wtp.wallpaper.controller.user;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtp.wallpaper.annotation.ReplaceURL;
import com.wtp.wallpaper.es.document.PictureDocument;
import com.wtp.wallpaper.pojo.entity.Picture;
import com.wtp.wallpaper.pojo.query.PageQuery;
import com.wtp.wallpaper.pojo.vo.PictureVO;
import com.wtp.wallpaper.pojo.result.Result;
import com.wtp.wallpaper.service.PictureService;
import com.wtp.wallpaper.es.service.PictureDocumentService;
import com.wtp.wallpaper.utils.PojoConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//@Api("壁纸相关接口")
@RestController
@Slf4j
@ReplaceURL
public class PictureController {

    @Autowired
    private PictureService pictureService;

    @Autowired
    private PictureDocumentService pictureDocumentService;


    //@ApiOperation("获取每日随机壁纸")
    @GetMapping("/randomWall")
    public Result randomWall(){
        return Result.success(pictureService.randomWall());
    }

    @GetMapping("/wallList")
    public Result wallList(@RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "5") int pageSize,
                           @RequestParam("classid") long classId){

        List<PictureVO> page = pictureService.getPageListByClassId(new PageQuery(pageNum,pageSize),classId);

        return Result.success(page);
    }

    @SentinelResource(value = "byResourceSentinelResource",
                    blockHandler = "handleException")
    @GetMapping("/downloadWall")
    public Result downloadWall(){
        return Result.success();
    }

    public Result handleException(BlockException exception)
    {
        return Result.error("下载频繁,请稍后再试!");
    }

    @GetMapping("/searchWall")
    public Result searchWall(@RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "5") int pageSize,
                             @RequestParam String keyword){

        //mysql模糊查询 全表扫描效率低
        //Page<Picture> searchWallByKeyword = pictureService.getSearchWallByKeyword(new PageQuery(pageNum,pageSize), keyword);

        //倒排索引查到ids
/*        List<Long> ids = pictureDocumentService.searchPicturesByTabsAndDescription(keyword);
        if(ids.isEmpty()){
            return Result.success(new ArrayList<PictureVO>());
        }*/

        List<PictureDocument> search = pictureDocumentService.search(keyword);
        List<Long> ids = search.stream().map(PictureDocument::getId).toList();
        if(ids.isEmpty()){
            return Result.success(new ArrayList<PictureVO>());
        }

        Map<Long, String> collect = search.stream().collect(Collectors.toMap(PictureDocument::getId, PictureDocument::getDescription));

        Page<Picture> searchWallByKeyword = pictureService.getPageListByPictureIds(
                new PageQuery(pageNum,pageSize)
                        .toMpPage("id",false)
                ,ids);

        List<Picture> records = searchWallByKeyword.getRecords();
        records = records.stream().peek(p -> p.setDescription(collect.get(p.getId()))).toList();

        return Result.success(PojoConvertUtil.convert(
                records,
                PictureVO.class));
    }
}
